package com.retail.order.controller;

import java.util.List;
import java.util.concurrent.Callable;
import javax.servlet.http.HttpServletResponse;

import com.retail.order.service.impl.OrderBatchService;
import com.retail.order.vo.VoOrderRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.retail.common.log.annotation.Log;
import com.retail.common.log.enums.BusinessType;
import com.retail.common.security.annotation.RequiresPermissions;
import com.retail.order.domain.TOrder;
import com.retail.order.service.ITOrderService;
import com.retail.common.core.web.controller.BaseController;
import com.retail.common.core.domain.Result;
import com.retail.common.core.utils.poi.ExcelUtil;
import com.retail.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author retail
 * @date 2024-02-27
 */
@RestController
@RequestMapping("/order")
public class TOrderController extends BaseController
{
    @Autowired
    private ITOrderService tOrderService;

    @Autowired
    private OrderBatchService orderBatchService;
    /**
     * 查询订单列表
     */
    @RequiresPermissions("order:order:list")
    @GetMapping("/list")
    public TableDataInfo list(TOrder tOrder)
    {
        startPage();
        List<TOrder> list = tOrderService.selectTOrderList(tOrder);
        return getDataTable(list);
    }


    @GetMapping("/orderList/{userId}")
    public Result<List<TOrder>> orderList(@PathVariable("userId") Integer userId) throws Exception {
        Callable<List<TOrder>> orderListTask = () -> {
            // 实际查询订单的方法，这里仅为示例
            List<TOrder> orders = orderBatchService.getOrderListFromSomewhere(userId);
            return orders;
        };
        // 执行Callable任务获取订单列表
        List<TOrder> orders = orderListTask.call();
        // 将获取到的订单列表封装到Result对象中并返回
        return Result.success(orders);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TOrder tOrder)
    {
        List<TOrder> list = tOrderService.selectTOrderList(tOrder);
        ExcelUtil<TOrder> util = new ExcelUtil<TOrder>(TOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @RequiresPermissions("order:order:query")
    @GetMapping(value = "/{orderId}")
    public Result getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(tOrderService.selectTOrderByOrderId(orderId));
    }

    /**
     * 新增订单
     */
    @RequiresPermissions("order:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody TOrder tOrder)
    {
        return toAjax(tOrderService.insertTOrder(tOrder));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("order:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody TOrder tOrder)
    {
        return toAjax(tOrderService.updateTOrder(tOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("order:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public Result remove(@PathVariable Long[] orderIds)
    {
        return toAjax(tOrderService.deleteTOrderByOrderIds(orderIds));
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @PutMapping("cancelOrder")
    public Result cancelOrder(Long orderId){
        return Result.success(tOrderService.cancelOrder(orderId));
    }

    @PutMapping("updateRemake")
    public Result updateRemake(TOrder tOrder){
        return Result.success(tOrderService.updateRemake(tOrder));

    }

}
